#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
检查WMS服务状态和API服务器状态
"""

import requests
import sys
from pathlib import Path

# 添加项目根目录到Python路径
current_dir = Path(__file__).parent.parent
sys.path.insert(0, str(current_dir))

from src.core.service_manager import ServiceManager
from src.core.config import config

def check_service_status():
    """检查服务状态"""
    print("========================================")
    print("WMS Simple 服务状态检查")
    print("========================================")
    print()
    
    # 检查Windows服务状态
    service_manager = ServiceManager(
        service_name="WMSSimpleService",
        display_name="WMS Simple 料位管理系统",
        description="WMS Simple 料位管理系统 - 自动化料位管理服务"
    )
    
    print("1. Windows服务状态:")
    service_status = service_manager.get_service_status()
    if service_status["success"]:
        print(f"   ✅ 服务状态: {service_status['status_text']}")
        print(f"   🔄 是否运行: {'是' if service_status['is_running'] else '否'}")
    else:
        print(f"   ❌ 服务状态: {service_status['message']}")
    print()
    
    # 检查API服务器状态
    print("2. API服务器状态:")
    api_url = f"http://{config.api_host}:{config.api_port}"
    try:
        response = requests.get(f"{api_url}/api/health", timeout=5)
        if response.status_code == 200:
            print(f"   ✅ API服务器运行正常")
            print(f"   🌐 地址: {api_url}")
            print(f"   📊 响应: {response.json()}")
        else:
            print(f"   ⚠️ API服务器响应异常: {response.status_code}")
    except requests.exceptions.ConnectionError:
        print(f"   ❌ API服务器无法连接: {api_url}")
    except requests.exceptions.Timeout:
        print(f"   ⏰ API服务器响应超时: {api_url}")
    except Exception as e:
        print(f"   ❌ 检查API服务器时发生错误: {e}")
    print()
    
    # 给出建议
    print("3. 运行建议:")
    if service_status.get("is_running"):
        print("   📋 Windows服务正在运行")
        print("   💡 建议: 使用API接口或Web界面进行操作")
        print("   ⚠️ 注意: 不要同时运行main_new.py，会导致端口冲突")
    else:
        print("   📋 Windows服务未运行")
        print("   💡 建议: 可以运行main_new.py进行手动操作")
        print("   🔧 或者: 启动Windows服务实现后台运行")

if __name__ == "__main__":
    check_service_status() 